package weaver.formmode.customjavacode.modeexpand.crm;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/*
 *@Description: 客户财务信息录入更新到客户库明细
 *@Param:
 *@return:
 *@Author: Dusongsong
 *@Date: 2020/10/26
 */
public class WeaCustomerLibraryAction extends AbstractModeExpandJavaCodeNew {
    /**
     * 执行模块扩展动作
     *
     * @param param param包含(但不限于)以下数据
     *              user 当前用户
     *              importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *              导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
     *              页面链接拼接的参数，比如b=2,可以通过param.get("b")来获取参数
     * @return
     */
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( "-----WeaCustomerLibraryAction-----" );
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        try {
            User user = (User) param.get( "user" );
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get( "RequestInfo" );
            if (requestInfo != null) {
                billid = Util.getIntValue( requestInfo.getRequestid() );
                modeid = Util.getIntValue( requestInfo.getWorkflowid() );
                if (billid > 0 && modeid > 0) {

                    String sql = "select nsr,swfitbm,khbmz,khxbm,phone,account_bank_number,account_of_the_bank,bank_name,invoicing_phone,address,yn_invoicing " +
                            "from uf_crm_fi_entry where id='" + billid + "'";
                    rs.executeQuery( sql );
                    rs.writeLog( sql );
                    if (rs.next()) {
                        String khbmz = Util.null2String( rs.getString( "khbmz" ) );//客户编号
                        String khxbm = Util.null2String( rs.getString( "khxbm" ) );//开户行编码
                        String swfitbm = Util.null2String( rs.getString( "swfitbm" ) );//SWFIT编码
                        String nsr = Util.null2String( rs.getString( "nsr" ) );//nsr
                        String phone = Util.null2String( rs.getString( "phone" ) );//电话
                        String account_bank_number = Util.null2String( rs.getString( "account_bank_number" ) );//开户账号
                        String account_of_the_bank = Util.null2String( rs.getString( "account_of_the_bank" ) );//开户银行
                        String bank_name = Util.null2String( rs.getString( "bank_name" ) );//银行名称
                        String invoicing_phone = Util.null2String( rs.getString( "invoicing_phone" ) );//预留电话
                        String address = Util.null2String( rs.getString( "address" ) );//地址
                        String yn_invoicing = Util.null2String( rs.getString( "yn_invoicing" ) );//电话
                        String mainid = getCode2Id( khbmz );//ID
                        String sql1 = "insert into uf_crm_customers_dt5 (mainid,khnsrlb,swfitbm,khxbm,dwdh,khxxh,khxjbzh,khxmc,kpdh,dwdz,sfmrkp)values(?,?,?,?,?,?,?,?,?,?,?)";
                        Object[] params = new Object[]{mainid, nsr, swfitbm, khxbm, phone, account_bank_number, account_of_the_bank, bank_name, invoicing_phone, address, yn_invoicing};
                        rs.writeLog(sql1, params);
                        if(rs.executeUpdate( sql1, params )){
                            rs.executeUpdate( "update uf_crm_fi_entry set yn_invoicing='1' where id !='" + billid + "'" );
                            rs.writeLog("update uf_crm_fi_entry set yn_invoicing='1' where id !='" + billid + "'");
                            rs.executeUpdate( "update uf_crm_customers_dt5 set sfmrkp='1' where id !=(select max(id) from uf_crm_customers_dt5 where mainid='" + mainid + "')");
                            rs.writeLog( "update uf_crm_customers_dt5 set sfmrkp='1' where id !=(select max(id) from uf_crm_customers_dt5 where mainid='" + mainid + "')");
                        }

                        //更新客户库（创达）
                        String cdID ="";//创达id
                        String selectSql = "select id from uf_CRM_KHKCD where customer_code='" + khbmz + "'";//获取客户库创达中主键
                        rs1.executeQuery( selectSql );
                        if (rs1.next()) {
                             cdID = Util.null2String( rs.getString( "id" ) );//客户编号
                            //更新客户库创达明细
                            String insertSql = "insert into uf_CRM_KHKCD_dt5 (mainid,khnsrlb,swfitbm,khxbm,dwdh,khxxh,khxjbzh,khxmc,kpdh,dwdz,sfmrkp)values(?,?,?,?,?,?,?,?,?,?,?)";
                            Object[] paramss = new Object[]{cdID, nsr, swfitbm, khxbm, phone, account_bank_number, account_of_the_bank, bank_name, invoicing_phone, address, yn_invoicing};
                            rs1.writeLog(insertSql, paramss);
                            if(rs1.executeUpdate(insertSql, paramss)){
                                rs1.executeUpdate( "update uf_CRM_KHKCD_dt5 set sfmrkp='1' where id !=(select max(id) from uf_CRM_KHKCD_dt5 where mainid='" + cdID + "')");
                                rs1.writeLog( "update uf_CRM_KHKCD_dt5 set sfmrkp='1' where id !=(select max(id) from uf_CRM_KHKCD_dt5 where mainid='" + cdID + "')");
                            }
                        }

                    }
                }
            }
        } catch (
                Exception e) {
            result.put( "errmsg", "自定义出错信息" );
            result.put( "flag", "false" );
        }
        return result;
    }

    public static String getCode2Id(String khbmz) {
        RecordSet rs = new RecordSet();
        String sql = "select id from uf_crm_customers where customer_code=?";
        rs.executeQuery( sql, khbmz );
        rs.next();
        String id = Util.null2String( rs.getString( "id" ) );
        return id;
    }
}